//
//  SearchTableViewCell.m
//  Cooking
//
//  Created by dlios on 15-10-7.
//  Copyright (c) 2015年 ZP.Y. All rights reserved.
//

#import "SearchTableViewCell.h"
#import "SearchModel.h"
#import "UIImageView+WebCache.h"
#import "PrefixHeader.pch"
#define SETCOLOR(R,G,B) [UIColor colorWithRed:(CGFloat)R/255 green:(CGFloat)G/255 blue:(CGFloat)B/255 alpha:1.0]
@interface SearchTableViewCell ()


@end;

@implementation SearchTableViewCell
- (void)dealloc
{
    [_myImageView release];
    [_myTitle release];
    [_description1 release];
    [_hard_level release];
    [_taste release];
    [_timeLable release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creat];
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
    
}
-(void)creat
{
    self.myImageView  = [[[UIImageView alloc]initWithFrame:CGRectMake(10*OffWidth, 10*OffHeight, 120*OffWidth, 100*OffHeight)]autorelease];
    
//    self.myImageView.frame =  CGRectMake(10, 5, 120, 110);
    [self.contentView addSubview:self.myImageView];
    
    
    self.title = [[[UILabel alloc]init]autorelease];
    self.description1 = [[[UILabel alloc]init]autorelease];
    self.hard_level = [[[UILabel alloc]init]autorelease];
    self.taste = [[[UILabel alloc]init]autorelease];
    self.timeLable = [[[UILabel alloc]init]autorelease];
    
    self.title.frame = CGRectMake(140*OffWidth, 5*OffHeight, 225*OffWidth, 25*OffHeight);
    self.description1.frame = CGRectMake(140*OffWidth, 30*OffHeight, 225*OffWidth, 40*OffHeight);
    self.description1.numberOfLines = 0;
    self.description1.font =[UIFont systemFontOfSize:14];
    self.hard_level.frame = CGRectMake(140*OffWidth, 70*OffHeight, 110*OffWidth, 25*OffHeight);
    self.hard_level.font =[UIFont systemFontOfSize:14];
    self.taste.frame = CGRectMake(255*OffWidth, 70*OffHeight, 110*OffWidth, 20*OffHeight);
    self.taste.font =[UIFont systemFontOfSize:15];
    self.timeLable.frame = CGRectMake(140*OffWidth, 95*OffHeight, 220*OffWidth, 20*OffHeight);
    self.timeLable.font =[UIFont systemFontOfSize:14];
    
    
    [self.contentView addSubview:self.title];
    [self.contentView addSubview:self.description1];
    [self.contentView addSubview:self.hard_level];
    [self.contentView addSubview:self.taste];
    [self.contentView addSubview:self.timeLable];
    
    
}
//
//-(void)layoutSubviews
//{
//    [super layoutSubviews];
//    self.myImageView.frame = CGRectMake(10, 5, 120, self.contentView.frame.size.height-10);
//    
//}

-(void)receveModel:(SearchModel *)model
{
    [self.myImageView sd_setImageWithURL:[NSURL URLWithString:model.image]placeholderImage:[UIImage imageNamed:@"zhanwei_01.png"]];
//    self.title.text = model.title;
    self.title.attributedText = [self colorData:model.title];
    self.description1.text = model.description1;
    
    NSString *hard =[NSString stringWithFormat:@"难度: %@",model.hard_level];
    self.hard_level.text =hard;
    
    NSString *taste =[NSString stringWithFormat:@"口味: %@",model.taste];
    self.taste.text = taste;
    
    NSString *time =[NSString stringWithFormat:@"烹饪时间: %@",model.cooking_time];
    self.timeLable.text = time;
}

- (NSMutableAttributedString *)colorData:(NSString *)withStr{
    NSMutableAttributedString *dataStr = [[NSMutableAttributedString alloc] initWithString:withStr];
    
    for (int i = 0; i < withStr.length - self.text.length + 1; i++) {
        
        if ([[withStr substringWithRange:NSMakeRange(i, self.text.length)] isEqualToString:self.text]) {
            
            NSRange range = NSMakeRange(i, self.text.length);
            [dataStr addAttribute:NSForegroundColorAttributeName value:SETCOLOR(255, 0, 0) range:NSMakeRange(range.location,range.length)];
            
        }
        
        
    }
    return dataStr;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];


    // Configure the view for the selected state
}

@end
